package com.persistance;

import com.metier.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.util.HibernateSession;

import java.util.*;

public class AccesData {

	private static Session session = HibernateSession.getSession();
	private static org.hibernate.Transaction trans = null;
	// accesseurs sur listes d'objets
	public static List<TypeCharge> getListeTypeCharge() {
		return session.createQuery("from TypeCharge").list();
	}

	public static List<Station> getListeStation() {
		return session.createQuery("from Station").list();
	}

	public static List<Borne> getListeBorne() {
		return session.createQuery("from Borne").list();
	}

	public static List<Borne> getListeBorneStation(int idStation) {
		return session.createQuery("from Borne where idStation="+idStation).list();
	}

	public static TypeCharge getTypeCharge(int idType) {
		return (TypeCharge) session.get(TypeCharge.class,idType);
	}

	public static Station getStation(int idStation) {
		return (Station) session.get(TypeCharge.class,idStation);
	}

	public static Borne getBorne(int idBorne) {
		return (Borne) session.get(Borne.class,idBorne);
	}

	public static boolean addStation(Station s) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.persist(s);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean deleteStation(Station s) {
		Boolean ok = false;
		try {
			
			trans = session.beginTransaction();
			session.delete(s);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean updateStation(Station s) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.update(s);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean addBorne(Borne b) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.persist(b);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean deleteBorne(Borne b) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.delete(b);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean updateBorne(Borne b) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.update(b);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean updateTypeCharge(TypeCharge t) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.update(t);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean deleteTypeCharge(TypeCharge t) {
		Boolean ok = false;
		try {
			trans = session.beginTransaction();
			session.delete(t);			
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}

	public static boolean addTypeCharge(TypeCharge t) {
		Boolean ok = false;
		try {			
			trans = session.beginTransaction();
			session.persist(t);
			trans.commit();
			ok = true;
		}
		catch(HibernateException e) {
			e.printStackTrace();
		}
		return ok;
	}	
}
